
//  创建reducer,不用写switch

const createReduce = (handler: any) => {
  return (state = { ...handler.state }, action: { type: string }) => {
    let newState = JSON.parse(JSON.stringify(state))
    for (let key in handler.actionNames) {
      if (action.type === handler.actionNames[key]) {
        handler.actions[handler.actionNames[key]](newState, action);
        break;
      }
    }
    return newState
  }
}


// 创建actionName
const createActionName = (actions: {}) => {
  let actionNames = {} // 定义一个全局的actionNames
  // actionNames有多少对键值对，取决于action里面有多少个函数。所以遍历store.actions，给actionNames添加键值对
  for (let key in actions) {
    actionNames[key] = key
  }
  return actionNames
}


export {
  createReduce,
  createActionName
}